package com.beiye.drivertransport.thematic.learning;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.beiye.drivertransport.R;
import com.beiye.drivertransport.SubActivity.SignActivity;
import com.beiye.drivertransport.activity.TwoBaseAty;
import com.beiye.drivertransport.bean.LoginUserBean;
import com.beiye.drivertransport.bean.ThematicLearnRecordBean;
import com.beiye.drivertransport.http.Course;
import com.beiye.drivertransport.utils.UserManger;
import com.githang.statusbar.StatusBarCompat;

import java.text.SimpleDateFormat;
import java.util.List;


import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Copyright (C)
 * Author: 刘学章
 * Date: 2021-3-13 17:22:03
 * Description:
 */
public class ThematicLearnRecordActivity extends TwoBaseAty {


    ImageView imgSafeback;

    RecyclerView acThematicLearnRecordRv;

    private static final int THEMATIC_LEARNRECORD_DATA=1;
    private String adId;
    private String rolename;

    @Override
    public int getLayoutId() {
        return R.layout.activity_thematic_learn_record;
    }

    @Override
    public void initData() {
        imgSafeback = (ImageView) findViewById(R.id.img_safeback);
        acThematicLearnRecordRv = (RecyclerView) findViewById(R.id.ac_thematicLearnRecord_rv);

        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#1F80C4"));
        Bundle bundle=getIntent().getExtras();
        adId=bundle.getString("adId");
        rolename=bundle.getString("rolename");
    }

    @Override
    public void requestData() {
        getThematicLearnRecord();
    }


    public void onClick(View view) {
        finish();
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        super.onSuccess(result, call, response, what);
        if(what==THEMATIC_LEARNRECORD_DATA){
            ThematicLearnRecordBean bean= JSON.parseObject(result,ThematicLearnRecordBean.class);
            List<ThematicLearnRecordBean.RowsBean> rowsBeans=bean.getRows();

            RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(this);
            acThematicLearnRecordRv.setLayoutManager(layoutManager);
            ThematicLearnRecordAdapter adapter=new ThematicLearnRecordAdapter(this,rowsBeans);
            acThematicLearnRecordRv.setAdapter(adapter);
        }
    }

    class ThematicLearnRecordAdapter extends RecyclerView.Adapter<ThematicLearnRecordAdapter.ViewHolder>{

        private Context context;
        private List<ThematicLearnRecordBean.RowsBean> lists;

        public ThematicLearnRecordAdapter(Context context,List<ThematicLearnRecordBean.RowsBean> lists){
            this.context=context;
            this.lists=lists;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view= LayoutInflater.from(context).inflate(R.layout.item_thematic_learn_record,viewGroup,false);
            return new ThematicLearnRecordAdapter.ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
            ThematicLearnRecordBean.RowsBean rowsBean=lists.get(i);
            viewHolder.stName.setText(rowsBean.getStName());
            viewHolder.chNo.setText(rowsBean.getChNo()+"学时");
            int finishMark=rowsBean.getFinishMark();
            int checkMark=rowsBean.getCheckMark();
            int sn=rowsBean.getSn();
            String orgId=rowsBean.getOrgId();
            String orgName=rowsBean.getOrgName();
            String userId=rowsBean.getUserId();
            int tmenuMark=rowsBean.getTmenuMark();
            int qmenuMark=rowsBean.getQmenuMark();
            int peMenuMark=rowsBean.getPeMenuMark();
            int emenuMark=rowsBean.getEmenuMark();
            int stId=rowsBean.getStId();
            String iconUrl2=rowsBean.getIconUrl2();


            viewHolder.optionBtn.setText("学习");
            viewHolder.auditStatus.setVisibility(View.GONE);
            viewHolder.showAudit.setVisibility(View.GONE);
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String finishDate=simpleDateFormat.format(rowsBean.getCreationDate());
            viewHolder.auditDate.setText("创建日期："+finishDate);

            if(finishMark==0){
                viewHolder.finishStatus.setText("未完成");
                viewHolder.finishStatus.setBackgroundResource(R.drawable.shape_hidden_red);
            }else if(finishMark==1){
                viewHolder.finishStatus.setText("已完成");
                viewHolder.finishStatus.setBackgroundResource(R.drawable.shape_hidden_green);
                viewHolder.optionBtn.setText("查看签名");
                viewHolder.auditStatus.setVisibility(View.VISIBLE);
                if(checkMark==0){
                    viewHolder.auditStatus.setText("待审核");
                    viewHolder.auditStatus.setBackgroundResource(R.drawable.shape_hidden_yellow);
                }else if(checkMark==1){
                    viewHolder.auditStatus.setText("未通过");
                    viewHolder.auditStatus.setBackgroundResource(R.drawable.shape_hidden_red);
                    viewHolder.showAudit.setVisibility(View.VISIBLE);
                    viewHolder.auditDesc.setText(rowsBean.getCheckDesc());

                }else if(checkMark==2){
                    viewHolder.auditStatus.setText("已审核");
                    viewHolder.auditStatus.setBackgroundResource(R.drawable.shape_hidden_green);
                }
            }

            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putString("orgId", orgId);
                    bundle.putInt("stId", stId);
                    bundle.putInt("position", i);
                    bundle.putInt("sn",sn);
                    bundle.putString("orgName", orgName);
                    //bundle.putString("iconUrl", iconUrl);
                    //bundle.putInt("ihveMark", ihveMark);
                    bundle.putString("adId",adId);
                    bundle.putString("rolename",rolename);
                    bundle.putInt("tmenuMark", tmenuMark);
                    bundle.putInt("qmenuMark", qmenuMark);
                    bundle.putInt("peMenuMark", peMenuMark);
                    bundle.putInt("emenuMark", emenuMark);
                    bundle.putString("iconUrl2",iconUrl2);
                    //bundle.putInt("seqTMark", seqTMark);
                    startActivity(ThematiclearningFinishedActivity.class,bundle);
                }
            });

            viewHolder.optionBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(finishMark==0){

                    }else if(finishMark==1){
                        startActivity(SignActivity.class,null);
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return lists.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            private TextView stName,chNo,auditDate,finishStatus,auditStatus,optionBtn,auditDesc;
            private LinearLayout showAudit;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                stName=itemView.findViewById(R.id.item_thematicLearnRecord_tv_stName);
                chNo=itemView.findViewById(R.id.item_thematicLearnRecord_tv_chNo);
                auditDate=itemView.findViewById(R.id.item_thematicLearnRecord_tv_auditDate);
                finishStatus=itemView.findViewById(R.id.item_thematicLearnRecord_tv_finishStatus);
                auditStatus=itemView.findViewById(R.id.item_thematicLearnRecord_tv_auditStatus);
                optionBtn=itemView.findViewById(R.id.item_thematicLearnRecord_tv_optionBtn);
                auditDesc=itemView.findViewById(R.id.item_thematicLearnRecord_tv_auditDesc);
                showAudit=itemView.findViewById(R.id.item_thematicLearnRecord_ll_showAudit);
            }
        }
    }

    //网络请求
    private void getThematicLearnRecord(){//获取专题学习记录
        LoginUserBean userInfo = UserManger.getUserInfo();
        String userId=userInfo.getData().getUserId();
        new Course().findByCond(userId,12,this,THEMATIC_LEARNRECORD_DATA);
    }
}
